import 'package:flutter/material.dart';
import 'package:mqshop/services/user_service.dart';
import 'package:mqshop/widget/mq_button.dart';

import '../../services/screen_adapter.dart';
import '../../services/user_service.dart';
import '../../services/event_bus.dart';

class UserPage extends StatefulWidget {
  UserPage({Key key}) : super(key: key);

  @override
  _UserPageState createState() => _UserPageState();
}

class _UserPageState extends State<UserPage> {

  bool _isLogin = false;
  List _userInfo = [];

  @override
  void initState() {
    super.initState();
    _getUserInfo();
    
    /// 监听登录页面改变的事件
    eventBus.on<UserEvent>().listen((event) {
      print(event.str);
      _getUserInfo();
    });
  }

  _getUserInfo() async{
    var isLogin = await UserService.getUserLoginState();
    var userInfo = await UserService.getUserInfo();

    setState(() {
      _isLogin = isLogin;
      _userInfo = userInfo;
    });
  }


  @override
  Widget build(BuildContext context) {

    ScreenAdapter.init();

    return Scaffold(
      // appBar: AppBar(
      //   title: Text('用户中心')
      // ),
      body: ListView(
        children: <Widget>[
          Container(
            height: ScreenAdapter.height(220),
            width: double.infinity,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('images/user_bg.jpg'),
                fit: BoxFit.cover
              )
            ),
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: ClipOval(
                    child: Image.asset(
                      'images/user.png', 
                      fit: BoxFit.cover, 
                      width: ScreenAdapter.width(100),
                      height: ScreenAdapter.height(100),
                    )
                  )
                ),
                !_isLogin ? Expanded(
                  flex: 1,
                  child: InkWell(
                    onTap: (){
                      Navigator.pushNamed(context, '/login');
                    },
                    child: Text('登录/注册', style: TextStyle(
                      color: Colors.white
                    ))
                  )
                ) :
                Expanded(
                  flex: 1,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text('用户名: ${_userInfo[0]['username']}', style: TextStyle(
                        color: Colors.white,
                        fontSize: ScreenAdapter.size(32)
                      )),
                      Text('普通会员', style: TextStyle(
                        color: Colors.white,
                        fontSize: ScreenAdapter.size(24)
                      )),
                    ]
                  )
                )
              ]
            )
          ),
          ListTile(
            leading: Icon(Icons.assessment, color: Colors.red),
            title: Text('全部订单'),
            onTap: (){
              Navigator.pushNamed(context, '/order');
            }
          ),
          Divider(),
          ListTile(
            leading: Icon(Icons.payment, color: Colors.green),
            title: Text('待付款')
          ),
          Divider(),
          ListTile(
            leading: Icon(Icons.local_car_wash, color: Colors.orange),
            title: Text('待收货')
          ),
          Container(
            height: 10,
            width: double.infinity,
            color: Color.fromRGBO(242, 242, 242, 0.9)
          ),
          ListTile(
            leading: Icon(Icons.favorite, color: Colors.lightGreen),
            title: Text('我的收藏')
          ),
          Divider(),
          ListTile(
            leading: Icon(Icons.people, color: Colors.black54),
            title: Text('在线客服')
          ),
          Divider(),
          _isLogin ? Container(
            padding: EdgeInsets.all(20),
            child: MqButton(
              color: Colors.red,
              text: '退出登录',
              callback: () async{
                UserService.loginOut();
                _getUserInfo();
              },
            )
          ) : Text('')
        ]
      )
    );
  }
}